package com.drought.rained.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.drought.rained.R;
import com.drought.rained.business.DialogManagerLJ;
import com.drought.rained.utils.ToastUtilsLJ;

public class GrantPermissionDialogLJ {
    private AlertDialog dialog;
    private GrantPermissionDialogCallBack callBack;

    public GrantPermissionDialogLJ(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.layout_grant_permission_dialog, null);
        TextView tv_title = view.findViewById(R.id.tv_tile);
        TextView tv_content = view.findViewById(R.id.tv_content);
        final CheckBox checkAgreement = view.findViewById(R.id.check_agreement);
        TextView tvAgreement = view.findViewById(R.id.tv_agreement);
        Button confirm = view.findViewById(R.id.confirm_button);
        final Button cancel = view.findViewById(R.id.cancel_button);
        tv_title.setText(context.getResources().getString(R.string.privacy_agreement_title));
        tv_content.setText(context.getResources().getString(R.string.privacy_agreement_content));
        builder.setView(view);
        builder.setCancelable(false);
        dialog = builder.create();

        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.dialog_transparent_bg_lj);
        // 不允许取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAgreement.setChecked(true);
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkAgreement.isChecked()) {
                    ToastUtilsLJ.showShort((Activity) context, R.string.privacy_agreement_has_read);
                    return;
                }
                if (callBack != null)
                    callBack.onConfirm();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null)
                    callBack.onCancel();
                hide();
            }

        });

    }

    public void show() {
        DialogManagerLJ.getManager().showDialogStack(dialog);
    }

    public void hide() {
        DialogManagerLJ.getManager().dismissDialog(dialog);
    }

    public void setOnClickListener(GrantPermissionDialogCallBack callBack) {
        this.callBack = callBack;
    }

    public interface GrantPermissionDialogCallBack {
        void onConfirm();

        void onCancel();
    }

}
